package com.sam.im.heiqun.uis.activities;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.amap.api.maps.AMap;
import com.amap.api.maps.CameraUpdateFactory;
import com.amap.api.maps.MapView;
import com.amap.api.maps.model.LatLng;
import com.amap.api.maps.model.Marker;
import com.amap.api.maps.model.MarkerOptions;
import com.sam.im.heiqun.R;
import com.yuyh.library.uis.activitys.BaseActivity;

import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class ToLocationActivity extends BaseActivity implements AMap.OnMapLoadedListener {


    @BindView(R.id.pre_v_back)
    ImageView preVBack;
    @BindView(R.id.pre_tv_title)
    TextView preTvTitle;
    @BindView(R.id.right)
    ImageView right;
    @BindView(R.id.amapview)
    MapView amapview;
    @BindView(R.id.tv_location)
    TextView tvLocation;
    @BindView(R.id.tv_addr)
    TextView tvAddr;
    @BindView(R.id.locatbt)
    Button locatbt;

    ImageView mapIcon1;
    TextView download1;
    CheckBox imgCheck1;
    RelativeLayout baiduitem;
    ImageView mapIcon2;
    TextView download2;
    CheckBox imgCheck2;
    RelativeLayout gaodeitem;

    private AMap aMap;
    private double lat;
    private double lut;
    private String addr;
    private Marker marker = null;
    private AlertDialog dialog = null;

    @Override
    public int getContentViewId() {
        return R.layout.activity_to_location;
    }

    @Override
    public void init(Bundle savedInstanceState) {
        preTvTitle.setText(getResources().getString(R.string.location_info));
        amapview.onCreate(savedInstanceState);
        if (aMap == null) {
            aMap = amapview.getMap();
        }
        if (aMap != null) {
            getlocationIntent();
        }
        initDialog();
    }



    private void initDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        View view = LayoutInflater.from(this).inflate(R.layout.location_dialog, null);
        mapIcon1 = (ImageView) view.findViewById(R.id.map_icon1);
        mapIcon2 = (ImageView) view.findViewById(R.id.map_icon2);
        download1 = (TextView) view.findViewById(R.id.download1);
        download2 = (TextView) view.findViewById(R.id.download2);
        imgCheck1 = (CheckBox) view.findViewById(R.id.img_check1);
        imgCheck2 = (CheckBox) view.findViewById(R.id.img_check2);
        baiduitem = (RelativeLayout) view.findViewById(R.id.baiduitem);
        gaodeitem = (RelativeLayout) view.findViewById(R.id.gaodeitem);
        download1.setOnClickListener(this);
        download2.setOnClickListener(this);
        imgCheck1.setOnClickListener(this);
        imgCheck2.setOnClickListener(this);
        baiduitem.setOnClickListener(this);
        gaodeitem.setOnClickListener(this);

        if (!isAvilible(this,"com.baidu.BaiduMap")){
            download1.setVisibility(View.VISIBLE);
            imgCheck1.setVisibility(View.GONE);
            baiduitem.setEnabled(false);
            imgCheck1.setEnabled(false);
            download1.setEnabled(true);
        }else {
            download1.setVisibility(View.GONE);
            imgCheck1.setVisibility(View.VISIBLE);
            imgCheck1.setEnabled(true);
            download1.setEnabled(false);
            baiduitem.setEnabled(true);
        }
        if (!isAvilible(this, "com.autonavi.minimap")){
            download2.setVisibility(View.VISIBLE);
            imgCheck2.setVisibility(View.GONE);
            imgCheck2.setEnabled(false);
            download2.setEnabled(true);
            gaodeitem.setEnabled(false);
        }else {
            download2.setVisibility(View.GONE);
            imgCheck2.setVisibility(View.VISIBLE);
            imgCheck2.setEnabled(true);
            download2.setEnabled(false);
            gaodeitem.setEnabled(true);
        }

        builder.setView(view);
        dialog = builder.create();

    }

    private void getlocationIntent() {
        Intent intent = getIntent();
        lat = intent.getDoubleExtra("lat", 0);
        lut = intent.getDoubleExtra("lut", 0);
        addr = intent.getStringExtra("addr");
//        LatLng latLng = new LatLng(lut, lat);
        LatLng latLng = new LatLng(lat, lut);
        tvAddr.setText(addr);
        marker = aMap.addMarker(new MarkerOptions().position(latLng).snippet("DefaultMarker").visible(true));
        aMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, 15));
    }

    @OnClick({R.id.pre_v_back, R.id.locatbt})
    public void onViewClick(View view) {
        switch (view.getId()) {
            case R.id.pre_v_back:
                finish();
                break;
            case R.id.locatbt:

                Window window = dialog.getWindow();
                window.setGravity(Gravity.CENTER);  //此处可以设置dialog显示的位置
               // window.setWindowAnimations(R.style.anim_push_out);  //添加动画
                dialog.show();
                //设置dialog的宽度和手机宽度一样
                WindowManager.LayoutParams lp = dialog.getWindow().getAttributes();
                lp.width = window.getWindowManager().getDefaultDisplay().getWidth()/3*2;
                dialog.getWindow().setAttributes(lp);//设置宽度
                break;
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        amapview.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        amapview.onPause();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        amapview.onDestroy();
    }


    @Override
    public void onMapLoaded() {
        aMap.moveCamera(CameraUpdateFactory.zoomTo(15));
    }

    @Override
    public void onClick(View view) {
        super.onClick(view);
        switch (view.getId()) {
            case R.id.download1:
                Uri baiduuri = Uri.parse("market://details?id=com.baidu.BaiduMap");
                Intent dowIntentbaidu = new Intent(Intent.ACTION_VIEW, baiduuri);
                startActivity(dowIntentbaidu);
                break;
            case R.id.img_check1:
            case R.id.baiduitem:
                imgCheck1.setChecked(true);
                imgCheck2.setChecked(false);
                try {
//                    Log.i("-----", "onClick: ======导航---百度===============lut="+lut+"===lat="+lat);
                    Intent  baidulocatintent = Intent.getIntent("intent://map/direction?" +
                            //"origin=latlng:"+"34.264642646862,108.95108518068&" +   //起点  此处不传值默认选择当前位置
                            "destination=latlng:"+lat+","+lut+"|name:"+addr+        //终点
//                            "destination=latlng:"+lut+","+lat+"|name:"+addr+        //终点
                            "&mode=driving" +          //导航路线方式
//                            "&region=成都" +           //
                            "&src=有慧科技|SAMIM#Intent;scheme=bdapp;package=com.baidu.BaiduMap;end");
//                    Intent  baidulocatintent = Intent.getIntent("intent://map/navi?" +
//                            //"origin=latlng:"+"34.264642646862,108.95108518068&" +   //起点  此处不传值默认选择当前位置
//                            "location="+lut+","+lat +        //终点
//                            "&src=有慧科技|SAMIM");
                    startActivity(baidulocatintent);
                } catch (URISyntaxException e) {
                    e.printStackTrace();
                }


                break;
            case R.id.map_icon2:
                break;
            case R.id.download2:
                Uri gaodeuri = Uri.parse("market://details?id=com.autonavi.minimap");
                Intent gaodeintent = new Intent(Intent.ACTION_VIEW, gaodeuri);
                startActivity(gaodeintent);
                break;
            case R.id.img_check2:
            case R.id.gaodeitem:
                imgCheck1.setChecked(false);
                imgCheck2.setChecked(true);
                try{
                    Intent  gaointent = Intent.getIntent("androidamap://navi?sourceApplication=EazyIm&poiname="+addr+"&lat="+lat+"&lon="+lut+"&dev=0&style=0");
                    startActivity(gaointent);
                } catch (URISyntaxException e)
                {e.printStackTrace(); }
                break;
        }
    }
    /* 检查手机上是否安装了指定的软件
    * @param context
    * @param packageName：应用包名
    * @return
            */
    public static boolean isAvilible(Context context, String packageName){
        //获取packagemanager
        final PackageManager packageManager = context.getPackageManager();
        //获取所有已安装程序的包信息
        List<PackageInfo> packageInfos = packageManager.getInstalledPackages(0);
        //用于存储所有已安装程序的包名
        List<String> packageNames = new ArrayList<String>();
        //从pinfo中将包名字逐一取出，压入pName list中
        if(packageInfos != null){
            for(int i = 0; i < packageInfos.size(); i++){
                String packName = packageInfos.get(i).packageName;
                packageNames.add(packName);
            }
        }
        //判断packageNames中是否有目标程序的包名，有TRUE，没有FALSE
        return packageNames.contains(packageName);
    }

}
